﻿// // ******************************************************************
// //       /\ /|       @file       AppSettingHelper.cs
// //       \ V/        @brief      await29
// //       | "")       @author     Await, 285283010@qq.com
// //       /  |
// //      /  \\        @Modified   2024-11-03 07:11
// //    *(__\_\        @Copyright  Copyright (c) , 2024-11-03 07:11 Await
// // ******************************************************************

namespace AutomaticSubtitles.Service.Infrastructure.Configuration;

public class AppSettingHelper
{
    static ConfigurationManager? Configuration { get; set; }

    public AppSettingHelper(ConfigurationManager configuration)
    {
        Configuration = configuration;
    }


    /// <summary>
    /// 获取配置文件中的值
    /// </summary>
    /// <typeparam name="T">配置值的类型</typeparam>
    /// <param name="keys">配置键</param>
    /// <returns>配置值</returns>
    public static T? GetConfigValue<T>(string keys, T? defaultValue = default)
    {
        try
        {
            if (Configuration != null)
            {
                var configurationSection = Configuration.GetSection(keys).Get<T>();
                return configurationSection ?? defaultValue;
            }
        }
        catch (Exception)
        {
            // 处理异常
        }
        return defaultValue; // 返回默认值
    }
    
    /// <summary>
    /// 获取配置文件中的值
    /// </summary>
    /// <typeparam name="T">配置值的类型</typeparam>
    /// <param name="keys">配置键</param>
    /// <returns>配置值</returns>
    public static string GetConfigValue(string keys)
    {
        try
        {
            if (Configuration != null)
            {
                return Configuration.GetSection(keys).Value ?? "";
            }
        }
        catch (Exception)
        {
            // 处理异常
        }
        return ""; // 返回默认值
    }
}